package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.mapper.JobTypeMapper;
import com.imooc.pojo.JobType;
import com.imooc.service.JobTypeService;
import com.imooc.vo.JobTypeSecondAndThirdVO;
import com.imooc.vo.JobTypeThirdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 职位类别 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2023-05-15
 */
@Service
public class JobTypeServiceImpl extends ServiceImpl<JobTypeMapper, JobType> implements JobTypeService {

    @Autowired
    private JobTypeMapper jobTypeMapper;

    @Override
    public boolean getJobTypeExistByName(String name) {
        JobType jobType = baseMapper.selectOne(new LambdaQueryWrapper<JobType>().eq(JobType::getName, name));
        return jobType != null;
    }

    @Override
    public void createNode(JobType jobType) {
        baseMapper.insert(jobType);
    }

    @Override
    public List<JobType> getFatherData(String id) {
        return baseMapper.selectList(new LambdaQueryWrapper<JobType>()
                .eq(JobType::getFatherId, id)
                .orderByAsc(JobType::getSort));
    }

    @Override
    public Long getChildrenCount(String id) {
        return baseMapper.selectCount(new LambdaQueryWrapper<JobType>().eq(JobType::getFatherId, id));
    }

    @Override
    public List<JobTypeSecondAndThirdVO> getSecondAndThirdListByTop(String id) {
        //通过一级id查询二级数据
        List<JobType> jobSecondsTypes = baseMapper.selectList(new LambdaQueryWrapper<JobType>().eq(JobType::getFatherId, id));
        List<String> secondIds = jobSecondsTypes.stream().map(JobType::getId).collect(Collectors.toList());
        List<JobTypeSecondAndThirdVO> lists = secondIds.stream().map((secondId) -> {
            JobType secondType = baseMapper.selectOne(new LambdaQueryWrapper<JobType>().eq(JobType::getId, secondId));
            List<JobType> thirdList = baseMapper.selectList(new LambdaQueryWrapper<JobType>().eq(JobType::getFatherId, secondId));

            List<JobTypeThirdVO> thirdVOList = thirdList.stream().map(thirdJobType -> {
                JobTypeThirdVO jobTypeThirdVO = new JobTypeThirdVO();
                jobTypeThirdVO.setThirdLevelId(thirdJobType.getId());
                jobTypeThirdVO.setThirdLevelName(thirdJobType.getName());
                jobTypeThirdVO.setThirdLevelSort(thirdJobType.getSort());
                return jobTypeThirdVO;
            }).collect(Collectors.toList());

            JobTypeSecondAndThirdVO vo = new JobTypeSecondAndThirdVO();
            vo.setSecondLevelName(secondType.getName());
            vo.setSecondLevelId(secondId);
            vo.setSecondLevelSort(secondType.getSort());
            vo.setJobTypeThirdVOList(thirdVOList);
            return vo;
        }).collect(Collectors.toList());

        return lists;
    }

    @Override
    public List<JobTypeSecondAndThirdVO> getSecondAndThirdListByTopBySQL(String id) {
        return jobTypeMapper.getSecondAndThirdListByTopBySQL(id);
    }


}
